package com.shd.edanpei.ui.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.bean.OrderDetailsModel;
import com.shd.edanpei.enums.StatusEnum;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by mc on 2018/9/20.
 * 我的订单 状态
 */

public class AuditDataView extends LinearLayout implements View.OnClickListener {

    private Unbinder mUnbinder;
    private OrderDetailsModel model;

    @BindView(R.id.root)
    View root;
    @BindView(R.id.tv_check1)
    TextView tvCheck;
    @BindView(R.id.iv_arrow1)
    ImageView ivArrow1;
    @BindView(R.id.cl_first_audit)
    View firstAudit;

    @BindView(R.id.tv_check2)
    TextView tvCheck2;
    @BindView(R.id.iv_arrow2)
    ImageView ivArrow2;
    @BindView(R.id.cl_end_audit)
    View endAudit;

    private int order1Type;

    TextView tvReason, tvReason2, tvMoney;
    View ll_tuikuan;


    public AuditDataView(Context context, OrderDetailsModel model, int order1Type) {
        super(context);
        this.model = model;
        this.order1Type = order1Type;
        initView();
    }


    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.audit_data_view, this);
        mUnbinder = ButterKnife.bind(this);
        tvReason = findViewById(R.id.tv_reason);
        tvReason2 = findViewById(R.id.tv_reason2);
        ll_tuikuan = findViewById(R.id.ll_tuikuan);
        tvMoney = findViewById(R.id.tv_money);

        int status = model.getBaseInfo().getUserStatus();

        if (status == StatusEnum.unpaid.getStatus()
                || status == StatusEnum.applying.getStatus()) {

        } else {
            if (status == StatusEnum.first.getStatus()) {
                //未出初审结果
                tvCheck.setText("暂未出结果");
                ivArrow1.setVisibility(INVISIBLE);
                findViewById(R.id.rl_first_audit).setVisibility(VISIBLE);
            } else if (status == StatusEnum.last.getStatus()) {
                //已出初审结果，未出终审结果
                findViewById(R.id.rl_first_audit).setOnClickListener(this);
                initFirstData();
                findViewById(R.id.rl_first_audit).setVisibility(VISIBLE);

                tvCheck2.setText("暂未出结果");
                ivArrow2.setVisibility(INVISIBLE);
                findViewById(R.id.rl_end_audit).setVisibility(VISIBLE);

            } else {
                if (!model.getFirstResult().isNull()) {
                    findViewById(R.id.rl_first_audit).setOnClickListener(this);
                    initFirstData();
                    findViewById(R.id.rl_first_audit).setVisibility(VISIBLE);

                }
                if (!model.getFinalResult().isNull()) {
                    //终审结果已出
                    findViewById(R.id.rl_end_audit).setOnClickListener(this);
                    initEndData();
                    findViewById(R.id.rl_end_audit).setVisibility(VISIBLE);
                }

            }

        }

        if (order1Type == Constant.ME_ORDER) {
            int status2 = model.getBaseInfo().getUserStatus();

            if (status2 == StatusEnum.cancel.getStatus()) {
                tvReason.setText("撤销理由：" + model.getApplyInfo().getCancelReason());
                tvReason.setVisibility(VISIBLE);
            } else if (status2 == StatusEnum.waitRepay.getStatus()
                    || status2 == StatusEnum.repaySuccess.getStatus()) {
                ll_tuikuan.setVisibility(VISIBLE);
                tvReason2.setText("退单理由：" + model.getApplyInfo().getCancelReason());
                tvMoney.setText("¥" + model.getBaseInfo().getPayDepositAmount());
            } else if (status2 == StatusEnum.refuse.getStatus()) {
                tvReason.setText("拒绝理由：" + model.getApplyInfo().getRefuseReason());
                tvReason.setVisibility(VISIBLE);
            }

        } else if (order1Type == Constant.SHARE_ORDER) {
            int bankStatus = model.getBaseInfo().getManagerStatus();

            if (bankStatus == StatusEnum.cancel.getStatus()) {
                tvReason.setText("撤销理由：" + model.getApplyInfo().getCancelReason());
                tvReason.setVisibility(VISIBLE);
            } else if (bankStatus == StatusEnum.refuse.getStatus()) {
                tvReason.setText("拒绝理由：" + model.getApplyInfo().getRefuseReason());
                tvReason.setVisibility(VISIBLE);
            }
        }


    }

    private void initFirstData() {
        ((TextView) findViewById(R.id.tv_first_amount)).setText(model.getFirstResult().getFirstAmount());
        ((TextView) findViewById(R.id.tv_first_rate)).setText(model.getFirstResult().getFirstRate());
        ((TextView) findViewById(R.id.tv_first_repayWay)).setText(model.getFirstResult().getFirstMode());
        ((TextView) findViewById(R.id.tv_first_repayDate)).setText(model.getFirstResult().getFirstTerm());
        ((TextView) findViewById(R.id.tv_first_loanDate)).setText(model.getFirstResult().getFirstDay());
    }

    private void initEndData() {
        ((TextView) findViewById(R.id.tv_end_amount)).setText(model.getFinalResult().getLastAmount());
        ((TextView) findViewById(R.id.tv_end_rate)).setText(model.getFinalResult().getLastRate());
        ((TextView) findViewById(R.id.tv_end_repayWay)).setText(model.getFinalResult().getLastMode());
        ((TextView) findViewById(R.id.tv_end_repayDate)).setText(model.getFinalResult().getLastTerm());
        ((TextView) findViewById(R.id.tv_end_loanDate)).setText(model.getFinalResult().getLastDay());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_first_audit:
                boolean isGone = firstAudit.getVisibility() == View.GONE;
                firstAudit.setVisibility(isGone ? VISIBLE : GONE);
                tvCheck.setText(isGone ? "收起" : "查看");
                ivArrow1.animate().setDuration(150).rotation(isGone ? 180 : 0).start();
                break;
            case R.id.rl_end_audit:
                boolean isGone2 = endAudit.getVisibility() == View.GONE;
                endAudit.setVisibility(isGone2 ? VISIBLE : GONE);
                tvCheck2.setText(isGone2 ? "收起" : "查看");
                ivArrow2.animate().setDuration(150).rotation(isGone2 ? 180 : 0).start();
                break;
        }
    }
}
